package cn.iocoder.yudao.module.custom.controller.admin.county;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.custom.controller.admin.county.vo.*;
import cn.iocoder.yudao.module.custom.dal.dataobject.county.CountyDO;
import cn.iocoder.yudao.module.custom.convert.county.CountyConvert;
import cn.iocoder.yudao.module.custom.service.county.CountyService;

@Tag(name = "管理后台 - 区县信息")
@RestController
@RequestMapping("/custom/county")
@Validated
public class CountyController {

    @Resource
    private CountyService countyService;

    @PostMapping("/create")
    @Operation(summary = "创建区县信息")
    @PreAuthorize("@ss.hasPermission('custom:county:create')")
    public CommonResult<Integer> createCounty(@Valid @RequestBody CountyCreateReqVO createReqVO) {
        return success(countyService.createCounty(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新区县信息")
    @PreAuthorize("@ss.hasPermission('custom:county:update')")
    public CommonResult<Boolean> updateCounty(@Valid @RequestBody CountyUpdateReqVO updateReqVO) {
        countyService.updateCounty(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除区县信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('custom:county:delete')")
    public CommonResult<Boolean> deleteCounty(@RequestParam("id") Integer id) {
        countyService.deleteCounty(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得区县信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('custom:county:query')")
    public CommonResult<CountyRespVO> getCounty(@RequestParam("id") Integer id) {
        CountyDO county = countyService.getCounty(id);
        return success(CountyConvert.INSTANCE.convert(county));
    }

    @GetMapping("/list")
    @Operation(summary = "获得区县信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('custom:county:query')")
    public CommonResult<List<CountyRespVO>> getCountyList(@RequestParam("ids") Collection<Integer> ids) {
        List<CountyDO> list = countyService.getCountyList(ids);
        return success(CountyConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得区县信息分页")
    @PreAuthorize("@ss.hasPermission('custom:county:query')")
    public CommonResult<PageResult<CountyRespVO>> getCountyPage(@Valid CountyPageReqVO pageVO) {
        PageResult<CountyDO> pageResult = countyService.getCountyPage(pageVO);
        return success(CountyConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出区县信息 Excel")
    @PreAuthorize("@ss.hasPermission('custom:county:export')")
    @OperateLog(type = EXPORT)
    public void exportCountyExcel(@Valid CountyExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<CountyDO> list = countyService.getCountyList(exportReqVO);
        // 导出 Excel
        List<CountyExcelVO> datas = CountyConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "区县信息.xls", "数据", CountyExcelVO.class, datas);
    }

    @GetMapping("/getCityLists/{parentId}")
    @Operation(summary = "获得商户信息分页")
    public CommonResult getCountyLists(@PathVariable Integer parentId) {
        List<CountyDO> countyLists = countyService.getCountyLists(parentId);
        return success(CountyConvert.INSTANCE.convertList(countyLists));
    }

}
